package com.foreveross.fetl.application.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.fetl.application.ITaskTemplateAssignDayApplication;
import com.foreveross.fetl.common.application.BaseApplicationImpl;
import com.foreveross.fetl.core.domain.task.TaskTemplateAssignDayEntity;
import com.foreveross.fetl.vo.TaskTemplateAssignDayVO;

@Transactional
@Service("taskTemplateAssignDayApplication")
public class TaskTemplateAssignDayApplicationImpl extends BaseApplicationImpl
		implements ITaskTemplateAssignDayApplication {

	@Override
	public void save(TaskTemplateAssignDayVO vo) {
		TaskTemplateAssignDayEntity entity = new TaskTemplateAssignDayEntity();
		try {
			entity.setId(vo.getId());
			entity.setAssignFlightDay(vo.getAssignFlightDay());
			entity.setAssignRoundDay(vo.getAssignRoundDay());
			entity.setDayType(vo.getDayType());
			entity.setFutureCycle(vo.getFutureCycle());
			entity.setTemplateId(vo.getTemplateId());
			entity.setStartDate(vo.getStartDate1());
			entity.setEndDate(vo.getEndDate1());
			entity.saveOrUpdate(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(TaskTemplateAssignDayVO vo) {
		TaskTemplateAssignDayEntity entity = new TaskTemplateAssignDayEntity();
		entity.getTaskTemplateAssignDayRepository()
				.createQuery(
						"delete TaskTemplateAssignDayEntity ttade where ttade.id= ?",
						new Long[] { vo.getId() }).executeUpdate();
	}

	@Override
	public TaskTemplateAssignDayVO get(Long id) {
		TaskTemplateAssignDayVO vo = new TaskTemplateAssignDayVO();
		TaskTemplateAssignDayEntity en = new TaskTemplateAssignDayEntity()
				.get(id);
		vo.setAssignFlightDay(en.getAssignFlightDay());
		vo.setAssignRoundDay(en.getAssignRoundDay());
		vo.setDayType(en.getDayType());
		vo.setEndDate(en.getEndDate());
		vo.setStartDate(en.getStartDate());
		vo.setFutureCycle(en.getFutureCycle());
		vo.setId(en.getId());
		return vo;
	}

	@Override
	public List<TaskTemplateAssignDayVO> findByCondition(Long templateId) {
		List<TaskTemplateAssignDayVO> listVO = new ArrayList<TaskTemplateAssignDayVO>();
		List<TaskTemplateAssignDayEntity> listEntity = new TaskTemplateAssignDayEntity()
				.createQuery(
						"select ttade from TaskTemplateAssignDayEntity ttade where ttade.templateId =?",
						new Object[] { templateId }).getResultList();
		if (null != listEntity && listEntity.size() > 0) {
			for (TaskTemplateAssignDayEntity en : listEntity) {
				TaskTemplateAssignDayVO vo = new TaskTemplateAssignDayVO();
				vo.setAssignFlightDay(en.getAssignFlightDay());
				vo.setAssignRoundDay(en.getAssignRoundDay());
				vo.setDayType(en.getDayType());
				vo.setEndDate(en.getEndDate());
				vo.setStartDate(en.getStartDate());
				vo.setFutureCycle(en.getFutureCycle());
				vo.setId(en.getId());
				vo.setTemplateId(en.getTemplateId());
				listVO.add(vo);
			}
		}
		return listVO;
	}

}
